package com.netease.research.common;

public class ServerResponse<T> {
    //    注意data是String的情况
    private T data;
    private String msg;
    private int statusCode;

//    构造方法


    private ServerResponse(int statusCode) {
        this.statusCode = statusCode;
    }

    public ServerResponse(int statusCode, T data) {
        this.data = data;
        this.statusCode = statusCode;
    }

    public ServerResponse(int statusCode, String msg) {
        this.msg = msg;
        this.statusCode = statusCode;
    }

    private ServerResponse(int statusCode, T data, String msg) {
        this.data = data;
        this.msg = msg;
        this.statusCode = statusCode;
    }

    public static <T> ServerResponse<T> createBySuccess() {
        return new ServerResponse<T>(StatusCode.SUCCESS.getCode());
    }

    public static <T> ServerResponse<T> createBySuccess(T data) {
        return new ServerResponse<T>(StatusCode.SUCCESS.getCode(), data);
    }

    public static <T> ServerResponse<T> createBySuccess(T data, String msg) {
        return new ServerResponse<T>(StatusCode.SUCCESS.getCode(), data,msg);
    }

    public static <T> ServerResponse<T> createBySuccessWithMsg(String msg) {
        return new ServerResponse<>(StatusCode.SUCCESS.getCode(), msg);
    }

    public static <T> ServerResponse<T> createByError() {
        return new ServerResponse<T>(StatusCode.ERROR.getCode());
    }

    public static <T> ServerResponse<T> createByErrorWithMsg(String msg) {
        return new ServerResponse<T>(StatusCode.ERROR.getCode(),msg);
    }

    public static <T> ServerResponse<T> createByCode(int statusCode){
        return new ServerResponse<>(statusCode);
    }

    public static <T> ServerResponse<T> createByCodeWithMsg(int statusCode ,String msg){
        return new ServerResponse<>(statusCode,msg);
    }

    public boolean isSuccess(){
        return this.statusCode==StatusCode.SUCCESS.getCode();
    }

    public T getData() {
        return data;
    }

    public String getMsg() {
        return msg;
    }

    public int getStatusCode() {
        return statusCode;
    }
}
